package cn.framelife.rainbow.util;

import java.io.File;
import java.util.List;
import java.util.Set;

/**
 * 字符串工具类
 * @author pgwei
 *
 */
public class StringUtil {
	/**
	 * 制表符
	 */
	public static String TAB="\t";
	/**
	 * 回车
	 */
	public static String ENTER = "\r\n";
	/**
	 * JS文件的新的一行
	 *
	 * @param tabNum 制表符的个数
	 * @return
	 */
	public static String newLineForJS(int tabNum){
		StringBuffer buffer = new StringBuffer(ENTER);
		for(int i = 0; i < tabNum; i++){
			buffer.append(TAB);
		}
		return buffer.toString();
	}
	/**
	 * 判断字符是否为空
	 * 
	 * @param str
	 * @return 是否为空
	 */
	public static Boolean isNotEmpty(String str) {
		if (str != null && str.trim().length() > 0
				&& !"null".equalsIgnoreCase(str)
				&& !"undefined".equalsIgnoreCase(str)) {
			return true;
		}
		return false;
	}
	
	/**
	 * 首字母转小写
	 *
	 * @Title: toLowerCaseFirstOne 
	 * @param @param s
	 * @param @return  
	 * @return String  
	 * @throws
	 */
    public static String toLowerCaseFirstOne(String s) {
        if(Character.isLowerCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1).toLowerCase()).toString();
    }
    
    /**
     * 首字母转大写
     *
     * @Title: toUpperCaseFirstOne 
     * @param @param s
     * @param @return  
     * @return String  
     * @throws
     */
    public static String toUpperCaseFirstOne(String s){
        if(Character.isUpperCase(s.charAt(0)))
            return s;
        else
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1).toLowerCase()).toString();
    }
    
    /**
     * 表的字段转化为类的变量
     *
     * @param columnName '_' 划分
     * @return
     */
    public static String columnName2ClassProperty(String columnName){
    	String[] names = columnName.toLowerCase().split("_");
    	StringBuffer buffer = new StringBuffer();
    	for(int i = 0; i < names.length; i++){
    		if(i == 0){
    			buffer.append(names[0]);
    		}else{
    			buffer.append(toUpperCaseFirstOne(names[i]));
    		}
    	}
    	
    	return buffer.toString();
    }
    
    /**
     * 表名转化为类名
     *
     * @param tableName '_' 划分
     * @return
     */
    public static String tableName2ClassName(String tableName){
    	String[] names = tableName.toLowerCase().split("_");
    	StringBuffer buffer = new StringBuffer();
    	for(String name : names){
    		buffer.append(toUpperCaseFirstOne(name));
    	}
    	return buffer.toString();
    }
    
    /**
     * 将多个字符串拼在一起
     *
     * @param strs
     * @return
     */
    public static String merge(String...strs){
    	StringBuffer buffer = new StringBuffer();
    	for(String str : strs){
    		buffer.append(replaceSeparator(str));
    	}
    	return buffer.toString();
    }
    
    /**
     * 更换路径分隔符
     *
     * @param str
     * @return
     */
    public static String replaceSeparator(String str){
    	String s = str.replace("/", File.separator).replace("\\", File.separator);
    	return s;
    }
    
    /**
     * List集合转换为字符口串，separator分隔
     *
     * @param list
     * @param separator
     * @return
     */
    public static String mergeListWithSeparator(List<String> list,String separator){
    	StringBuffer buffer = new StringBuffer();
		for(int i=0; i < list.size(); i++){
			if(i != 0){
				buffer.append(separator);
			}
			String actionPackage = list.get(i);
			buffer.append(actionPackage);
		}
		
		return buffer.toString();
    }
    
    /**
     * List集合转换为字符口串，separator分隔
     *
     * @param list
     * @param separator
     * @return
     */
    public static String mergeSetWithSeparator(Set<String> set,String separator){
    	StringBuffer buffer = new StringBuffer();
    	int i = 0;
    	for(String actionPackage : set){
    		if(i != 0){
    			buffer.append(separator);
    		}
    		buffer.append(actionPackage);
    		i++;
    	}
    	return buffer.toString();
    }
}
